package cn.itrip.controller;

import cn.itrip.beans.dto.Dto;
import cn.itrip.beans.vo.comment.ItripScoreCommentVO;
import cn.itrip.beans.vo.comment.ItripSearchCommentVO;
import cn.itrip.common.DtoUtil;
import cn.itrip.common.Page;
import cn.itrip.service.ItripCommentService;
import cn.itrip.service.ItripHotelService;
import cn.itrip.service.ItripImageService;
import cn.itrip.service.ItripLabelDicService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/api/comment")
public class SystemCommentController {

    @Resource
    private ItripCommentService itripCommentService;


    @RequestMapping(value = "/gethotelscore/{hotelId}",method= RequestMethod.GET,produces = "application/json")
    @ResponseBody
    public Dto<Object> getHotelScore(@PathVariable String hotelId){
        Dto<Object> dto = new Dto<Object>();
        if(null != hotelId && !"".equals(hotelId)){
            ItripScoreCommentVO itripScoreCommentVO = new ItripScoreCommentVO();
            try {
                itripScoreCommentVO =  itripCommentService.getAvgAndTotalScore(Long.valueOf(hotelId));
                dto = DtoUtil.returnSuccess("获取评分成功",itripScoreCommentVO);
            } catch (Exception e) {
                e.printStackTrace();
                dto = DtoUtil.returnFail("获取评分失败","100001");
            }
        }else{
            dto = DtoUtil.returnFail("hotelId不能为空","100002");
        }
        return dto;
    }

    @RequestMapping(value = "/getcount/{hotelId}",method=RequestMethod.GET,produces = "application/json")
    @ResponseBody
    public Dto<Object> getCommentCountByType(@PathVariable String hotelId){
        Dto<Object> dto = new Dto<Object>();
        Integer count = 0;
        Map<String,Integer> countMap = new HashMap<>();
        Map<String,Object> param = new HashMap<String,Object>();
        if(null != hotelId && !"".equals(hotelId)){
            param.put("hotelId",hotelId);
            count = getItripCommentCountByMap(param);
            if(count != -1){
                countMap.put("allcomment",count);
            }else{
                return DtoUtil.returnFail("获取酒店总评论数失败","100014");
            }
            param.put("isOk",1);//0：有待改善 1：值得推荐
            count = getItripCommentCountByMap(param);
            if(count != -1){
                countMap.put("isok",count);
            }else{
                return DtoUtil.returnFail("获取酒店值得推荐评论数失败","100017");
            }
            param.put("isOk",0);//0：有待改善 1：值得推荐
            count = getItripCommentCountByMap(param);
            if(count != -1){
                countMap.put("improve",count);
            }else{
                return DtoUtil.returnFail("获取酒店有待改善评论数失败","100016");
            }
            param.put("isHavingImg",1);//0:无图片 1:有图片
            param.put("isOk",null);
            count = getItripCommentCountByMap(param);
            if(count != -1){
                countMap.put("havingimg",count);
            }else{
                return DtoUtil.returnFail("获取酒店有图片评论数失败","100015");
            }
        }else{
            return DtoUtil.returnFail("参数hotelId为空","100018");
        }
        dto = DtoUtil.returnSuccess("获取酒店各类评论数成功",countMap);
        return dto;
    }
    public Integer getItripCommentCountByMap(Map<String,Object> param){
        Integer count  = -1;
        try {
            count =  itripCommentService.getItripCommentCountByMap(param);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return count;
    }

    @RequestMapping(value = "/getcommentlist",method=RequestMethod.POST,produces = "application/json")
    @ResponseBody
    public Dto<Object> getCommentList(@RequestBody ItripSearchCommentVO itripSearchCommentVO){
        Dto<Object> dto = new Dto<Object>();
        Map<String,Object> param=new HashMap<>();
        if(itripSearchCommentVO.getIsOk() == -1){
            itripSearchCommentVO.setIsOk(null);
        }
        if(itripSearchCommentVO.getIsHavingImg() == -1){
            itripSearchCommentVO.setIsHavingImg(null);
        }
        param.put("hotelId",itripSearchCommentVO.getHotelId());
        param.put("isHavingImg",itripSearchCommentVO.getIsHavingImg());
        param.put("isOk",itripSearchCommentVO.getIsOk());
        try{
            Page page = itripCommentService.queryItripCommentPageByMap(param,
                    itripSearchCommentVO.getPageNo(),
                    itripSearchCommentVO.getPageSize());
            dto = DtoUtil.returnDataSuccess(page);
        }catch (Exception e){
            e.printStackTrace();
            dto = DtoUtil.returnFail("获取评论列表错误","100020");
        }

        return dto;
    }
}
